/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MBuscarComprobante.java
 *
 * Created on 05/10/2013, 07:51:36 AM
 */
package Transaction;

import Clases.MCCliente;
import Clases.MCComprobante;
import Clases.MCVenta;
import Modelo.Conection;
import Presentation.Main;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JDesktopPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Marco
 */
public class MBuscarComprobante extends javax.swing.JInternalFrame {

    DefaultTableModel modelo = new DefaultTableModel();
    Conection Cone=new Conection();
    public static int b=0; 
    Main menu;

    
    /** Creates new form MBuscarComprobante */
    public MBuscarComprobante(Main ppanel) throws SQLException {
        initComponents();
        menu=ppanel;
        //  codigo para mostrar los datos en la tabla 
        Cone.Conectar();
//        panel=p;
//        idventa="";
        ArrayList<MCComprobante> listaContartos = Cone.MrecuperarContratasActivas();
        jTable1.removeAll();
        modelo.addColumn("ID VENTA");
        modelo.addColumn("DNI o RUC");
        modelo.addColumn("NOMBRE");
        modelo.addColumn("FECHA");
        Object[] fila = new Object[4];
        for (int i = 0; i < listaContartos.size(); i++) {
            MCComprobante C = listaContartos.get(i);
            String fecha=C.getFecha();
            String fecha_actual=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            System.out.println(fecha_actual+"--"+fecha);
            if (fecha.equals(fecha_actual)){
                fila[0]= C.getId_venta();
                fila[1]= C.getDni_ruc();
                fila[2]= C.getNombre();
                fila[3]= C.getFecha();
                modelo.addRow(fila);
            }
        }
        jTable1.setModel(modelo);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jButton1.setText("SELECCIONAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("buscar por");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Numero", "DNI o RUC", "Cliente", "Fecha" }));

        jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextField1CaretUpdate(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ID VENTA", "DNI o RUC", "NOMBRE", "FECHA"
            }
        ));
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(100);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(100);
        jTable1.getColumnModel().getColumn(3).setMaxWidth(100);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 772, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addGap(11, 11, 11)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextField1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextField1CaretUpdate
        // TODO add your handling code here:
        String texto=jTextField1.getText();
        String criterio=jComboBox1.getSelectedItem().toString();
        DefaultTableModel tabla_modelaux=new DefaultTableModel();
        
        tabla_modelaux.addColumn("ID VENTA");
        tabla_modelaux.addColumn("DNI o RUC");
        tabla_modelaux.addColumn("NOMBRE");
        tabla_modelaux.addColumn("FECHA");
        int nro_col=0;
        
        if(criterio.equals("Numero")){nro_col=0;}
        else if(criterio.equals("DNI o RUC")){nro_col=1;}
        else if(criterio.equals("Cliente")){nro_col=2;}
        else if(criterio.equals("Fecha")){nro_col=3;}
        for(int i=0;i<modelo.getRowCount();i++){
            if(modelo.getValueAt(i, nro_col).toString().contains(texto))
            {
                Object[] fil=new Object[6];
                fil[0]=modelo.getValueAt(i, 0).toString();
                fil[1]=modelo.getValueAt(i, 1).toString();
                fil[2]=modelo.getValueAt(i, 2).toString();
                fil[3]=modelo.getValueAt(i, 3).toString();
                tabla_modelaux.addRow(fil);            
            }        
        }
        jTable1.setModel(tabla_modelaux);

    // TODO add your handling code here:
}//GEN-LAST:event_jTextField1CaretUpdate

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
//        try {
//            // TODO add your handling code here:
//            int fila = jTable1.getSelectedRow();
//            String idventa= jTable1.getValueAt(fila, 0).toString();
//            MModificarcomprobante fmModificar = new MModificarcomprobante(idventa);            
////            Presentation.Main.panelDesktop.removeAll();
////            tablaform fmBuscar= new tablaform();
////            Presentation.Main.panelDesktop.add(fmModificar);
//            try {
//              fmModificar.setMaximum(true);  
//            } catch (Exception e) {
//            }
//            fmModificar.show();
//            this.dispose();
//        } catch (SQLException ex) {
//            ex.printStackTrace();
//        }
//
//

        try {
            int fila = jTable1.getSelectedRow();
            String idventa= jTable1.getValueAt(fila, 0).toString();
            menu.panelDesktop.removeAll();
            Venta_recuperada fmBuscar= new Venta_recuperada(menu,idventa);
           // tablaform fmBuscar= new tablaform();
           menu.panelDesktop.add(fmBuscar);
            try {
              fmBuscar.setMaximum(true);  
            } catch (Exception e) {
                e.printStackTrace();
            }
            fmBuscar.show();

        // TODO add your handling code here:
        } catch (SQLException ex) {
            
        }



}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
